<template>
  <!-- 逻辑：在这个页面获取数据，通过props传参的方式给各个组件传参 -->
  <div class="report">
    <tableTool>
      <div slot="export">
        <el-button type="primary" @click="excelExport">导出</el-button>
      </div>
      <div class="tool-form" slot="form">
        <div class="input-group">
          <span class="lable">设备名称：</span>
          <el-input placeholder="请输入" v-model="filterEquipmentName" />
        </div>
        <div class="input-group">
          <span class="lable">设备类别：</span>
          <el-input placeholder="请输入" v-model="filterCategoryName" />
        </div>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="success" @click="onReset">重置</el-button>
      </div>
    </tableTool>
    <customTable :list="list" :combined="combined">
      <template v-slot:title>
        <tr>
          <th colspan="13">项目部</th>
        </tr>
        <tr>
          <th>聘用方式</th>
          <th>姓名</th>
          <th>性别</th>
          <th>年龄</th>
          <th style="width: 100px"></th>
          <th>操作范围</th>
          <th>操作手类型</th>
          <th>持证编号</th>
          <th>发证机关</th>
          <th>发证时间</th>
          <th>复审时间</th>
          <th>证件有效期</th>
          <th>联系方式</th>
        </tr>
      </template>
      <template #body="{ list }">
        <tr v-for="item in list" :key="item.id">
          <td>{{ item.a }}</td>
          <td>{{ item.b }}</td>
          <td>{{ item.c }}</td>
          <td>{{ item.d }}</td>
          <td>{{ item.e }}</td>
          <td>{{ item.f }}</td>
          <td>{{ item.g }}</td>
          <td>{{ item.h }}</td>
          <td>{{ item.i }}</td>
          <td>{{ item.j }}</td>
          <td>{{ item.k }}</td>
          <td>{{ item.l }}</td>
          <td>{{ item.m }}</td>
        </tr>
      </template>
      <template #empty>
        <tr>
          <td colspan="13" class="empty">暂无数据</td>
        </tr>
      </template>
      <template #footer="{ combined }">
        <tr>
          <td>合计</td>
          <td>{{ combined.b }}</td>
          <td>{{ combined.c }}</td>
          <td>{{ combined.d }}</td>
          <td>{{ combined.e }}</td>
          <td>{{ combined.f }}</td>
          <td>{{ combined.g }}</td>
          <td>{{ combined.h }}</td>
          <td>{{ combined.i }}</td>
          <td>{{ combined.j }}</td>
          <td>{{ combined.k }}</td>
          <td>{{ combined.l }}</td>
          <td>{{ combined.m }}</td>
        </tr>
      </template>
    </customTable>
    <pagination @onCurrentChange="handleCurrentChange" :total="page.total"></pagination>
  </div>
</template>
<script>
import tableTool from '../components/tableTool.vue'
import customTable from '../components/customTable.vue'
import pagination from '../components/pagination.vue'
import { combinedFn } from '@/pages/comprehensiveReport/equipmentManagement/mixin'
import request from '@/api/comprehensiveReport/equipmentManagement.js'
export default {
  name: 'specialEquipmentOperatorListProject',
  components: { tableTool, customTable, pagination },
  data () {
    return {
      list: [],
      orgName: '',
      page: {
        total: 0,
        currentPage: 1,
        limit: 10,
      },
      combined: {},
      headers: {
        // 定义对应关系- 把英文的key转化成中文的key所对应的值
        聘用方式: 'a',
        姓名: 'b',
        性别: 'c',
        年龄: 'd',
        '': 'e',
        操作范围: 'f',
        操作手类型: 'g',
        持证编号: 'h',
        发证机关: 'i',
        发证时间: 'j',
        复审时间: 'k',
        证件有效期: 'l',
        联系方式: 'm',
      },
      filterEquipmentName: '',
      filterCategoryName: ''
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const orgId = this.$route.query.orgId || localStorage.orgId
      this.orgName = this.$route.query.orgName || this.$store.state.userInfo.orgInfo.orgName
      // 携带参数：currentPage(当前页)，limit(每页显示多少条)
      const data = await request.getequipmentExpenseLedgerListMOCK({
        page: this.page.currentPage,
        limit: this.page.limit,
        orgId,
      })
      console.log('发送请求，获取数据', data)
      if (data.list.length == 0) {
        this.list = []
        this.combined = {}
        this.page.total = 0
        return;
      }
      this.page.total = data.totalCount
      const { list, combined } = combinedFn(Object.values(this.headers).slice(1), data.list)
      this.list = list
      this.combined = combined
    },
    handleCurrentChange (currentPage) {
      this.page.currentPage = currentPage
      this.getList()
    },
    excelExport () {
      const multiHeader = [[this.orgName, '', '', '', '', '', '', '', '', '', '', '', '']] // 它是一个二维数组
      const merges = ['A1:M1']
      // 懒加载 script-loader
      import('@/utils/Export2Excel.js').then(async (excel) => {
        const orgId = this.$route.query.orgId || localStorage.orgId
        // 获取所有的数据
        const { list } = await request.getequipmentExpenseLedgerListMOCK({
          page: 1,
          limit: this.page.total,
          orgId,
        })
        // list就是所有的数据，list [{},{},{}] 转换为 => [[],[],[],[]]
        const data = this.formatJson(this.headers, list) // 转化数据表结构
        const { combined } = combinedFn(Object.values(this.headers).slice(1), data.list)
        data.push(['合计', ...Object.values(combined)])
        excel.export_json_to_excel({
          header: Object.keys(this.headers),
          data,
          filename: '设备管理-特种（通用）设备操作人员名单-' + this.orgName,
          autoWidth: true,
          bookType: 'xlsx',
          multiHeader,
          merges,
        })
      })
    },
    formatJson (headers, list) {
      return list.map((item) => {
        return Object.values(headers).map((value) => {
          return item[value]
        })
      })
    },
  },
}
</script>
<style lang="scss">

</style>
